package com.mask.roll.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mask.roll.common.code.ServiceEnums;
import com.mask.roll.common.constant.RollConstants;
import com.mask.roll.common.util.RollResponse;
import com.mask.roll.edu.entity.Teacher;
import com.mask.roll.edu.request.TeacherQueryRequest;
import com.mask.roll.edu.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author hx
 * @since 2021-10-20
 */
@RestController
@RequestMapping("/edu/teacher")
@Api(tags = "讲师管理")
@Slf4j
public class TeacherController {

    @Resource
    private TeacherService teacherService;

    /**
     * 条件查询讲师表所有数据
     * @return list
     */
    @PostMapping("/get/active/{number}/{size}")
    @ApiOperation("条件获取所有讲师分页列表")
    public RollResponse getActive(
            @ApiParam("页码") @PathVariable("number") Integer number,
            @ApiParam("页数") @PathVariable("size") Integer size,
            @RequestBody(required = false) TeacherQueryRequest request){
        Page<Teacher> page = new Page<>(number,size);
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        //条件匹配
        if (!StringUtils.isEmpty(request.getLevel())){
            wrapper.eq("level",request.getLevel());
        }
        if (!StringUtils.isEmpty(request.getBegin())){
            wrapper.ge("gmt_create",request.getBegin());
        }
        if (!StringUtils.isEmpty(request.getEnd())){
            wrapper.le("gmt_create",request.getEnd());
        }
        if (!StringUtils.isEmpty(request.getName())){
            wrapper.like("name",request.getName());
        }
        //按照创建时间降序排列
        wrapper.orderByDesc("gmt_create");
        IPage<Teacher> pageList = teacherService.page(page, wrapper);
        return RollResponse.result(ServiceEnums.EDU_TEACHER_LIST.getCode(),
                RollConstants.INFO, ServiceEnums.EDU_TEACHER_LIST.getMessage(),pageList);
    }

    @ApiOperation(value = "获取所有讲师列表")
    @GetMapping("/get/all")
    public RollResponse getAll() {
        List<Teacher> list = teacherService.list(null);
        return RollResponse.result(ServiceEnums.EDU_TEACHER_LIST.getCode(),
                RollConstants.SUCCESS,ServiceEnums.EDU_TEACHER_LIST.getMessage(),list);
    }


    /**
     * 逻辑删除讲师
     * @param id 讲师id
     * @return boolean
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除讲师")
    public RollResponse deleteTeacher(@ApiParam(value = "讲师id",required = true) @PathVariable("id") String id){
        boolean b = teacherService.removeById(id);
        if (b){
            return RollResponse.result(ServiceEnums.EDU_TEACHER_DELETE_SUCCESS.getCode(),
                    RollConstants.INFO,ServiceEnums.EDU_TEACHER_DELETE_SUCCESS.getMessage());
        }else {
            return RollResponse.result(ServiceEnums.EDU_TEACHER_DELETE_FAILED.getCode(),
                    RollConstants.ERROR,ServiceEnums.EDU_TEACHER_DELETE_FAILED.getMessage());
        }
    }

    @PostMapping("/add")
    @ApiOperation("添加讲师")
    public RollResponse add(@RequestBody Teacher teacher){
        boolean flag = teacherService.save(teacher);
        if (flag){
            log.info("添加讲师成功:{}",teacher);
            return RollResponse.result(ServiceEnums.EDU_TEACHER_ADD_SUCCESS.getCode(),
                    RollConstants.INFO,ServiceEnums.EDU_TEACHER_ADD_SUCCESS.getMessage());
        }else {
            return RollResponse.result(ServiceEnums.EDU_TEACHER_ADD_FAILED.getCode(),
                    RollConstants.WARN,ServiceEnums.EDU_TEACHER_ADD_FAILED.getMessage());
        }
    }

    @GetMapping("/get/{id}")
    @ApiOperation("根据id查找讲师")
    public RollResponse getTeacher(@PathVariable("id") String id){
        Teacher teacher = teacherService.getById(id);
        return RollResponse.result(ServiceEnums.EDU_TEACHER_GET.getCode(),
                RollConstants.INFO,ServiceEnums.EDU_TEACHER_GET.getMessage(),teacher);
    }

    @PostMapping("/update")
    @ApiOperation("修改讲师")
    public RollResponse updateTeacher(@RequestBody Teacher teacher){
        boolean b = teacherService.updateById(teacher);
        if (b){
            log.info("修改讲师成功{}",teacher);
            return RollResponse.result(ServiceEnums.EDU_TEACHER_UPDATE_SUCCESS.getCode(),
                    RollConstants.INFO, ServiceEnums.EDU_TEACHER_UPDATE_SUCCESS.getMessage(),teacher);
        }else {
            return RollResponse.result(ServiceEnums.EDU_TEACHER_UPDATE_FAILED.getCode(),
                    RollConstants.ERROR,ServiceEnums.EDU_TEACHER_DELETE_FAILED.getMessage());
        }
    }


}

